﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace CommandBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public RoutedCommand MyCommand { get; set; }

        private bool Test { get; set; }

        public MainWindow()
        {
            InitializeComponent();
        }

        public override void EndInit()
        {
            base.EndInit();
            
            #region question 4
            MyCommand = new RoutedCommand();
            this.CommandBindings.Add(new System.Windows.Input.CommandBinding(MyCommand, MyCommand_Executed, MyCommand_CanExecute));
            this.MyButton.Command = this.MyCommand;
            #endregion question 4

            #region question 5
            var textBlock = new TextBlock();
            textBlock.Text = "Save";
            var button = new Button();
            button.Content = textBlock;
            this.stackPanel.Children.Add(button);
            #endregion question 5

        }

        private void MyCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (this.Test)
            {
                e.CanExecute = true;
                e.Handled = true;
            }
        }

        private void MyCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("My command executed!");
        }

        private void Help_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
            e.Handled = true;
        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            this.Test = !this.Test;

            this.listBox.Items.Add(DateTime.Now.Ticks);
            var templateOne = this.FindResource("templateOne");
            this.listBox.ItemTemplate = (DataTemplate) templateOne;
        }
    }
}
